﻿using System;
using System.Collections.Generic;
using System.Text;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKArch.Utils;
using YJKDragCurveJig;

namespace YJKArch.SlopeDim
{
	internal class SlopeDimHelper
	{
		public static void CreateDimData(SlopeDim slopDim, Autodesk.Revit.DB.View view, ref XYZ ptBase, ref List<JigEdgeInfo> curveInfoList, ref List<JigTextInfo> textInfoList, ref List<CurveLoop> clList)
		{
			double num = (double)view.Scale / 100.0;
			XYZ zero = XYZ.Zero;
			ptBase = zero;
			TextNoteType type = ((ShowType)slopDim.cbxStyle.SelectedItem).Type;
			double num2 = type.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble();
			double num3 = type.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
			string text = slopDim.cbxValue.Text;
			byte[] bytes = Encoding.Default.GetBytes(text);
			double num4 = num2 * num3;
			if (num4 < 1.0)
			{
				num4 = 1.0;
			}
			double num5 = (double)bytes.Length * num4 / 1.7 * 3.0 * num;
			double num6 = type.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble();
			double lineWidth = (double)SlopeDimHelper.GetLength(text) * num6;
			double num7 = Convert.ToDouble(slopDim.txtAngle.Text);
			double num8 = num7 / 180.0 * Math.PI;
			XYZ startPoint = new XYZ(zero.X, zero.Y, zero.Z);
			XYZ endPoint = new XYZ(zero.X + num5, zero.Y, zero.Z);
			Line line = YJKLineEx.YJKGetBound(startPoint, endPoint);
			double num9 = line.Length / 3.5;
			double num10 = line.Length / 11.0;
			double num11 = line.Length / 6.0;
			XYZ xyz = LineAssistFunc.Midpoint(line);
			XYZ xyz2 = line.GetEndPoint(0);
			XYZ xyz3 = line.GetEndPoint(1);
			XYZ xyz4 = XYZ.Zero;
			XYZ xyz5 = XYZ.Zero;
			if (slopDim.cbxArrow.SelectedItem.ToString() == "实心箭头" || slopDim.cbxArrow.SelectedItem.ToString() == "空心箭头")
			{
				xyz4 = new XYZ(xyz2.X + num9, xyz2.Y + num10, 0.0);
				xyz5 = new XYZ(xyz2.X + num9, xyz2.Y - num10, 0.0);
			}
			else if ((num7 >= 90.0 && num7 <= 180.0) || (num7 >= 180.0 && num7 < 270.0))
			{
				xyz4 = new XYZ(xyz2.X + num9, xyz2.Y - num10, 0.0);
				xyz5 = new XYZ(xyz2.X + num9, xyz2.Y, 0.0);
			}
			else
			{
				xyz4 = new XYZ(xyz2.X + num9, xyz2.Y + num10, 0.0);
				xyz5 = new XYZ(xyz2.X + num9, xyz2.Y, 0.0);
			}
			Line line2 = YJKLineEx.YJKGetBound(xyz2, xyz4);
			Line line3 = YJKLineEx.YJKGetBound(xyz4, xyz5);
			Line line4 = YJKLineEx.YJKGetBound(xyz5, xyz2);
			xyz2 = Geometry.RotateTo(xyz2, xyz, XYZ.BasisZ, -num8);
			xyz3 = Geometry.RotateTo(xyz3, xyz, XYZ.BasisZ, -num8);
			Curve curve = YJKLineEx.YJKGetBound(xyz2, xyz3);
			JigEdgeInfo item = new JigEdgeInfo(xyz2, xyz3, 0.0);
			curveInfoList.Add(item);
			line2 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line2.GetEndPoint(0), xyz, XYZ.BasisZ, -num8), Geometry.RotateTo(line2.GetEndPoint(1), xyz, XYZ.BasisZ, -num8));
			line3 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line3.GetEndPoint(0), xyz, XYZ.BasisZ, -num8), Geometry.RotateTo(line3.GetEndPoint(1), xyz, XYZ.BasisZ, -num8));
			line4 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line4.GetEndPoint(0), xyz, XYZ.BasisZ, -num8), Geometry.RotateTo(line4.GetEndPoint(1), xyz, XYZ.BasisZ, -num8));
			JigEdgeInfo item2 = new JigEdgeInfo(line2.GetEndPoint(0), line2.GetEndPoint(1), 0.0);
			JigEdgeInfo item3 = new JigEdgeInfo(line3.GetEndPoint(0), line3.GetEndPoint(1), 0.0);
			JigEdgeInfo item4 = new JigEdgeInfo(line4.GetEndPoint(0), line4.GetEndPoint(1), 0.0);
			curveInfoList.Add(item2);
			curveInfoList.Add(item3);
			curveInfoList.Add(item4);
			if (slopDim.cbxArrow.SelectedItem.ToString() == "实心箭头" || slopDim.cbxArrow.SelectedItem.ToString() == "实心半箭头")
			{
				CurveLoop curveLoop = new CurveLoop();
				curveLoop.Append(line2);
				curveLoop.Append(line3);
				curveLoop.Append(line4);
				clList.Add(curveLoop);
			}
			XYZ xyz6 = (curve.GetEndPoint(1) - xyz).Normalize() * num11;
			XYZ baseVec;
			XYZ xyz7;
			if ((num7 >= 90.0 && num7 <= 180.0) || (num7 >= 180.0 && num7 < 270.0))
			{
				xyz6 = Geometry.RotateTo(xyz6, -Math.PI*.5, XYZ.BasisZ);
				baseVec = Geometry.RotateTo(new XYZ(-1.0, 0.0, 0.0), -num8, XYZ.BasisZ);
				xyz7 = Geometry.RotateTo(new XYZ(0.0, -1.0, 0.0), -num8, XYZ.BasisZ);
			}
			else
			{
				xyz6 = Geometry.RotateTo(xyz6, Math.PI*.5, XYZ.BasisZ);
				baseVec = Geometry.RotateTo(new XYZ(1.0, 0.0, 0.0), -num8, XYZ.BasisZ);
				xyz7 = Geometry.RotateTo(new XYZ(0.0, 1.0, 0.0), -num8, XYZ.BasisZ);
			}
			double num12 = type.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble() * (double)view.Scale * 1.2;
			XYZ origin = xyz + xyz6 + xyz7 * num12;
			JigTextInfo item5 = new JigTextInfo(type, origin, baseVec, xyz7, (Autodesk.Revit.DB.TextAlignFlags)128, lineWidth, text);
			textInfoList.Add(item5);
		}

		private static int GetLength(string str)
		{
			if (str.Length == 0)
			{
				return 0;
			}
			Encoding encoding = new ASCIIEncoding();
			int num = 0;
			byte[] bytes = encoding.GetBytes(str);
			for (int i = 0; i < bytes.Length; i++)
			{
				if (bytes[i] == 63)
				{
					num += 2;
				}
				else
				{
					num++;
				}
			}
			return num;
		}

		public static bool CreateDim(Autodesk.Revit.DB.Document m_doc, List<JigEdgeInfo> curveInfoList, List<JigTextInfo> textInfoList, List<CurveLoop> clList, XYZ ptInsert, ref ElementSet newgrpElements)
		{
			Autodesk.Revit.DB.View activeView = m_doc.ActiveView;
			Transform identity = Transform.Identity;
			identity.Origin = ptInsert;
			identity.BasisX = activeView.RightDirection;
			identity.BasisY = activeView.UpDirection;
			identity.BasisZ = -activeView.ViewDirection;
			for (int i = 0; i < curveInfoList.Count; i++)
			{
				JigEdgeInfo jigEdgeInfo = curveInfoList[i];
				XYZ startPoint = Geometry.TransformPoint(jigEdgeInfo.StartPoint, identity);
				XYZ endPoint = Geometry.TransformPoint(jigEdgeInfo.EndPoint, identity);
				Curve curve = YJKLineEx.YJKGetBound(startPoint, endPoint);
				DetailCurve detailCurve = m_doc.Create.NewDetailCurve(activeView, curve);
				newgrpElements.Insert(detailCurve);
				for (int j = 0; j < RevitVersionFuncs.LineStyles(m_doc, detailCurve).Size; j++)
				{
					GraphicsStyle graphicsStyle = RevitVersionFuncs.LineStyles(m_doc, detailCurve).get_Item(j) as GraphicsStyle;
					int hashCode = -2000044.GetHashCode();
					if (i > 0)
					{
						hashCode = -2000042.GetHashCode();
					}
					if (graphicsStyle.GraphicsStyleCategory.GetHashCode() == hashCode)
					{
						detailCurve.LineStyle = graphicsStyle;
						break;
					}
				}
			}
			foreach (JigTextInfo jigTextInfo in textInfoList)
			{
				XYZ origin = Geometry.TransformPoint(jigTextInfo.Origin, identity);
				XYZ baseVec = jigTextInfo.BaseVec;
				XYZ upVec = jigTextInfo.UpVec;
				TextNote textNode = m_doc.GetTextNode(activeView, origin, baseVec, upVec, jigTextInfo.LineWidth, jigTextInfo.TextAlign, jigTextInfo.StrText);
				if (textNode != null)
				{
					newgrpElements.Insert(textNode);
					textNode.TextNoteType = jigTextInfo.theTextNoteType;
					textNode.GetParameter(BuiltInParameter.KEEP_READABLE).Set(0);
					m_doc.Regenerate();
				}
			}
			if (clList.Count > 0)
			{
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(m_doc);
				filteredElementCollector.OfClass(typeof(FilledRegionType));
				FilledRegionType filledRegionType = null;
				foreach (Element element in filteredElementCollector.ToElements())
				{
					if (element.Name == "black")
					{
						filledRegionType = (element as FilledRegionType);
						break;
					}
				}
				if (filledRegionType == null)
				{
					ElementId fillPatternId = new ElementId(19);
					filledRegionType = ((filteredElementCollector.ToElements()[0] as FilledRegionType).Duplicate("black") as FilledRegionType);
					FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(m_doc);
					filteredElementCollector2.OfClass(typeof(FillPatternElement));
					foreach (Element element2 in filteredElementCollector2.ToElements())
					{
						if (element2.Name == "实体填充")
						{
							fillPatternId = element2.Id;
							break;
						}
					}
					filledRegionType.FillPatternId = fillPatternId;
				}
				ElementId id = filledRegionType.Id;
				try
				{
					FilledRegion filledRegion = FilledRegion.Create(m_doc, id, activeView.Id, clList);
					ElementTransformUtils.MoveElement(m_doc, filledRegion.Id, ptInsert);
					newgrpElements.Insert(filledRegion);
				}
				catch
				{
				}
			}
			return true;
		}
	}
}
